<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Http\Requests\OrdersAdd;
use App\Http\Requests\OrdersCommentAdd;
use App\Models\Goods;
use App\Models\Orders;
use App\Models\OrdersComment;
use App\Models\OrdersGoods;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class OrdersController extends Controller
{
    // 订单列表
    public function index()
    {
        $result = Orders::orderByDesc('id')
            ->where('user_id', auth('wechat')->id())
            ->with('goods')
            ->paginate($this->limit);
        return $this->writeJson(0, $result->items());
    }

    // 创建订单
    public function add(OrdersAdd $request)
    {
        $address_id = $request->post('address_id');
        $goods_info = $request->post('goods_info');
        /**
         $goods_info = [
             [
                 'goods_id' => 1,
                 'buy_count' => 1
             ]
         ];
         */
        Orders::add(auth('wechat')->id(), $address_id, $goods_info);
    }

    // 确认收货
    public function over(Request $request)
    {
        $id = $request->post('id');
        if (empty($id)) {
            return $this->writeJson(500, null, '参数错误');
        }

        $orders = Orders::whereUserId(auth('wechat')->id())
            ->where('id', $id)
            ->first();
        if (empty($orders)) {
            return $this->writeJson(500, null, '订单不存在');
        }
        if ($orders->status != 2 || $orders->is_pay != 1) {
            return $this->writeJson(500, null, '非法操作');
        }
        $orders->status = 3;
        $orders->overd_at = now();
        $orders->save();
        return $this->writeJson(0, null, '收货成功');
    }

    // 删除订单
    public function del(Request $request)
    {
        $id = $request->post('id');
        if (empty($id)) {
            return $this->writeJson(500, null, '参数错误');
        }

        $orders = Orders::whereUserId(auth('wechat')->id())
            ->where('id', $id)
            ->first();
        if (empty($orders)) {
            return $this->writeJson(500, null, '订单不存在');
        }
        if (in_array($orders->status, [1, 2])) {
            return $this->writeJson(500, null, '暂时无法删除订单');
        }

        try {
            DB::beginTransaction();
            OrdersGoods::whereOrdersId($orders->id)->delete();
            $orders->delete();
            DB::commit();
            return $this->writeJson(0, null, '删除成功');
        } catch (\Exception $exception) {
            \Log::error($exception);
            DB::rollBack();
        } catch (\Throwable $e) {
            \Log::error($e);
            DB::rollBack();
        }
        return $this->writeJson(500, null, '删除失败');
    }

    // 评论订单
    public function comment(OrdersCommentAdd $request)
    {
        /**
        $comment_info = [
        [
        'goods_id' => 1,
        'content' => '很可以',
        'score' => 5
        ]
        ];
         */
        $comment_info = $request->post('comment_info');
        $orders_id = $request->post('orders_id');
        $user_id = auth('wechat')->id();

        $orders = Orders::whereUserId($user_id)
            ->where('id', $orders_id)
            ->first();
        if (empty($orders)) {
            return $this->writeJson(500, null, '订单不存在');
        }
        if ($orders->status != 3) {
            return $this->writeJson(500, null, '订单还不能评价');
        }

        try {
            DB::beginTransaction();
            foreach ($comment_info as $info) {
                $model = new OrdersComment();
                $model->user_id = $user_id;
                $model->orders_id = $orders_id;
                $model->content = $info['content'];
                $model->score = $info['score'];
                $model->goods_id = $info['goods_id'];
                $model->save();

                // 好评增加好评数
                if ($model->score >= 4) {
                    Goods::whereId($info['goods_id'])->increment('praise_num');
                }
            }

            $orders->is_comment = 1;
            $orders->status = 4;
            $orders->save();
            DB::commit();

            return $this->writeJson(0, null, '评论成功');
        } catch (\Exception $exception) {
            \Log::error($exception);
            DB::rollBack();
        } catch (\Throwable $throwable) {
            \Log::error($throwable);
            DB::rollBack();
        }
        return $this->writeJson(500, null, '评论失败');
    }
}
